package com.open.capacity.workflow.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.PageUtil;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow.dao.WFDelegateRuleDao;
import com.open.capacity.workflow.dao.WFDelegateRuleRelationDao;
import com.open.capacity.workflow.model.WFDelegateRule;
import com.open.capacity.workflow.model.WFDelegateRuleRelation;
import com.open.capacity.workflow.service.WFDelegateRuleService;
import com.open.capacity.workflow.vo.WFDelegateRuleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class WFDelegateRuleServiceImpl implements WFDelegateRuleService {

    @Autowired
    private WFDelegateRuleDao delegateRuleDao;

    @Autowired
    private WFDelegateRuleRelationDao delegateRuleRelationDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(WFDelegateRule delegateRule) {

        delegateRuleDao.save(delegateRule);
        log.info("保存委托规则：{}", delegateRule);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(WFDelegateRule delegateRule) {

        delegateRuleDao.updateByOps(delegateRule);
        log.info("修改委托规则：{}", delegateRule);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        WFDelegateRule delegateRule = delegateRuleDao.findById(id);

        delegateRuleDao.delete(id);
        delegateRuleRelationDao.deleteByRuleId(id);

        log.info("删除委托规则：{}", delegateRule);
    }


    @Override
    public WFDelegateRule findById(String id) {
        return delegateRuleDao.findById(id);
    }

    @Override
    public PageResult<WFDelegateRule> pageDelegateRules(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<WFDelegateRule>  list = delegateRuleDao.findList(params);
        PageInfo<WFDelegateRule> pageInfo = new PageInfo(list);

        return PageResult.<WFDelegateRule>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    @Override
    public List<WFDelegateRule> findDelegateRules(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<WFDelegateRule> list = delegateRuleDao.findList(params);
        return list;
    }


    @Override
    public Result saveOrUpdate(WFDelegateRuleVo delegateRule, String keyValue) {
        int i = 0;
        LoginAppUser currentUser = SysUserUtil.getLoginAppUser();
        if (StringUtils.isNotEmpty(keyValue)) {
            WFDelegateRule model = delegateRuleDao.findById(keyValue);
            if (model != null) {
                delegateRule.setId(keyValue);
                i = delegateRuleDao.updateByOps(delegateRule);
            }
        } else {
            delegateRule.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            delegateRule.setCreateUserid(currentUser.getId());
            delegateRule.setCreateUsername(currentUser.getUsername());
            delegateRule.setCreateTime(new Date());
            i = delegateRuleDao.save(delegateRule);
        }
        WFDelegateRuleRelation relation = null;
        for (String schemeinfoId : delegateRule.getSchemeinfoList()) {
            relation = new WFDelegateRuleRelation();
            relation.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            relation.setDelegateRuleId(delegateRule.getId());
            relation.setSchemeinfoId(schemeinfoId);
            delegateRuleRelationDao.save(relation);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
